package com.bysj.studentsystem.controller;

import com.bysj.studentsystem.vo.ResponseDataVO;
import com.bysj.studentsystem.vo.SysExamVO;
import com.bysj.studentsystem.entity.SysExam;
import com.bysj.studentsystem.service.SysExamService;
import com.github.pagehelper.PageInfo;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * (SysExam)综合评测控制层
 *
 * @author makejava
 * @since 2024-03-09 16:51:48
 */
@RestController
@RequestMapping("exam")
public class SysExamController {
    /**
     * 服务对象
     */
    @Resource
    private SysExamService sysExamService;

    /**
     * 分页查询
     *
     * @return 查询结果
     */
    @GetMapping
    public ResponseDataVO<PageInfo<SysExamVO>> queryByPage(@RequestParam(value = "userId", required = false) Integer userId,
                                                           @RequestParam(value = "examName", required = false) String examName,
                                                           @RequestParam(value = "examContent", required = false) String examContent,
                                                           @RequestParam(value = "pageNum", required = false) Integer pageNum,
                                                           @RequestParam(value = "pageSize", required = false) Integer pageSize) {
        if (pageNum == null || pageSize == null) {
            pageNum = 1;
            pageSize = 10;
        }
        SysExam sysExam = new SysExam();
        sysExam.setUserId(userId);
        sysExam.setExamName(examName);
        sysExam.setExamContent(examContent);
        return new ResponseDataVO().ok(this.sysExamService.queryByPage(sysExam, pageNum, pageSize));
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("{id}")
    public ResponseDataVO<SysExam> queryById(@PathVariable("id") Integer id) {
        return new ResponseDataVO().ok(this.sysExamService.queryById(id));
    }

    /**
     * 新增数据
     *
     * @param sysExam 实体
     * @return 新增结果
     */
    @PostMapping
    public ResponseDataVO<SysExam> add(@RequestBody SysExam sysExam) {
        return new ResponseDataVO().ok(this.sysExamService.insert(sysExam));
    }

    /**
     * 编辑数据
     *
     * @param sysExam 实体
     * @return 编辑结果
     */
    @PutMapping
    public ResponseDataVO<SysExam> edit(@RequestBody SysExam sysExam) {
        return new ResponseDataVO().ok(this.sysExamService.update(sysExam));
    }

    /**
     * 删除数据
     *
     * @param id 主键
     * @return 删除是否成功
     */
    @DeleteMapping
    public ResponseDataVO<Boolean> deleteById(@RequestParam("id") Integer id) {
        return new ResponseDataVO().ok(this.sysExamService.deleteById(id));
    }

}

